package com.hujf.flink.window;

import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor;
import org.apache.flink.streaming.api.windowing.time.Time;

/**
 * @Author hujf
 * @Date 2022/4/19 10:48
 * @Description TODO
 */
public class EventTime {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment environment = StreamExecutionEnvironment.getExecutionEnvironment();
        environment.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);

        SingleOutputStreamOperator<String> stringSingleOutputStreamOperator = environment.socketTextStream("127.0.0.1", 9999,"\n").assignTimestampsAndWatermarks(
                new BoundedOutOfOrdernessTimestampExtractor<String>(Time.milliseconds(5000)) {
                    @Override
                    public long extractTimestamp(String element) {
                        String[] s = element.split(" ");
                        return Long.parseLong(s[0]);
                    }
                }
        );

        stringSingleOutputStreamOperator.print();
        environment.execute();
    }
}
